
package com.be.beadmin.material.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.basicData.controller.MaterialInfoController;
import com.be.beadmin.basicData.service.MaterialInfoService;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.design.entity.ProductBomOverview;
import com.be.beadmin.material.dto.input.PickingReccieveDetailsDto;
import com.be.beadmin.material.dto.output.MaterialComparisonEx;
import com.be.beadmin.material.dto.output.PickingReccieveDetailsEx;
import com.be.beadmin.material.entity.MaterialCodeInfo;
import com.be.beadmin.material.entity.PickingOverView;
import com.be.beadmin.material.entity.PickingReccieveDetails;
import com.be.beadmin.material.entity.PickingReccieveDetails_;
import com.be.beadmin.material.mapper.PickingReccieveDetailsMapper;
import com.be.beadmin.material.mapper.PickingReccieveDetailsMapperEx;
import com.be.beadmin.material.repository.PickingOverViewRepository;
import com.be.beadmin.material.repository.PickingReccieveDetailsRepository;
import com.be.beadmin.material.service.PickingReccieveDetailsService;
import com.be.beadmin.product.entity.WorkOrderInfor;
import com.be.beadmin.product.service.WorkOrderInforService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import javax.validation.constraints.Null;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author zmy
* @date 2023-05-14
**/
@Service
@RequiredArgsConstructor
public class PickingReccieveDetailsServiceImpl implements PickingReccieveDetailsService {

    @Autowired
    private PickingReccieveDetailsRepository pickingReccieveDetailsRepository;
    @Autowired
    private PickingOverViewRepository pickingOverViewRepository;
    @Autowired
    private PickingReccieveDetailsMapper pickingReccieveDetailsMapper;
    @Autowired
    private PickingReccieveDetailsMapperEx pickingReccieveDetailsExMapper;
    @Autowired
    private WorkOrderInforService workOrderInforService;
    @Resource
    private MaterialInfoService materialInfoService;


    @Override
    public Page<PickingReccieveDetailsEx> getPickingReccieveDetailsByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<PickingReccieveDetails> page = pickingReccieveDetailsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(PickingReccieveDetails_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(PickingReccieveDetails_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<PickingReccieveDetailsEx> page1 = page.map(pickingReccieveDetailsExMapper::toEntity);
            return page1;

    }

    @Override
    public void addPickingReccieveDetails(PickingReccieveDetailsDto pickingReccieveDetailsDto) {
            PickingReccieveDetails result = pickingReccieveDetailsRepository.findPickingReccieveDetailsByIdAndDelete(pickingReccieveDetailsDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("PickingReccieveDetails不存在,请重新查询");
            }
            PickingReccieveDetails pickingReccieveDetails = pickingReccieveDetailsMapper.toEntity(pickingReccieveDetailsDto);
            pickingReccieveDetailsRepository.save(pickingReccieveDetails);
    }

    @Override
    public void updatePickingReccieveDetails(PickingReccieveDetailsDto pickingReccieveDetailsDto) {
            PickingReccieveDetails pickingReccieveDetails = pickingReccieveDetailsRepository.findPickingReccieveDetailsById(pickingReccieveDetailsDto.getId());
            pickingReccieveDetails = pickingReccieveDetailsMapper.toEntity(pickingReccieveDetailsDto);
            pickingReccieveDetailsRepository.save(pickingReccieveDetails);
    }


    @Override
    public void delPickingReccieveDetails(String id) {
        PickingReccieveDetails pickingReccieveDetails = pickingReccieveDetailsRepository.findPickingReccieveDetailsById(id);
        pickingReccieveDetails.setDelete(1);
        pickingReccieveDetailsRepository.save(pickingReccieveDetails);
    }

    @Override
    public void delPickingReccieveDetailss (List<String> ids) { }

    @Override
    public List<MaterialComparisonEx> getMaterialComparisonsByProductCode(String productCode) {
        List<PickingReccieveDetails> pickingReccieveDetailsList = pickingReccieveDetailsRepository.findPickingReccieveDetailsByProductCodeAndDelete(productCode , 0);
        List<MaterialComparisonEx> materialComparisonExList = new ArrayList<>();
        for(int i=0 ; i < pickingReccieveDetailsList.size() ; i++){
            PickingReccieveDetails pickingReccieveDetails = pickingReccieveDetailsList.get(i);
            MaterialComparisonEx materialComparisonEx = new MaterialComparisonEx();
            String materialName = materialInfoService.getMaterialNameByMaterialCode(pickingReccieveDetails.getMaterialCode());
            String materialSpec = materialInfoService.getMaterialSpecByMaterialCode(pickingReccieveDetails.getMaterialCode());
            materialComparisonEx.setPlanMaterialName(materialName);
            materialComparisonEx.setPlanMaterialSpec(materialSpec);
            materialComparisonEx.setPlanMaterialCode(pickingReccieveDetails.getMaterialCode());
            materialComparisonEx.setPlanMaterialQty(pickingReccieveDetails.getMaterialNeedQty());
            materialComparisonEx.setRealMaterialName(materialName);
            materialComparisonEx.setRealMaterialSpec(materialSpec);
            materialComparisonEx.setRealMaterialCode(pickingReccieveDetails.getMaterialCode());
            materialComparisonEx.setRealMaterialQty(pickingReccieveDetails.getMaterialNeedQty());
            materialComparisonExList.add(materialComparisonEx);
        }
        return materialComparisonExList;
    }

}